﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CuaHangLaptop.DAL;

namespace CuaHangLaptop.BIZ
{
    public class HoaDon_BIZ
    {
        int id;
        string maHoaDon;
        DateTime ngaylap;
        decimal tongtien;
        KhachHang_BIZ khachhang = new KhachHang_BIZ();
        bool tinhtrang;

        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        public string MaHoaDon
        {
            get { return maHoaDon; }
            set { maHoaDon = value; }
        }

        public DateTime NgayLap
        {
            get { return ngaylap; }
            set { ngaylap = value; }
        }

        public decimal TongTien
        {
            get { return tongtien; }
            set { tongtien = value; }
        }

        public KhachHang_BIZ KhachHang
        {
            get { return khachhang; }
            set { khachhang = value; }
        }

        public string MaKH
        {
            get { return khachhang.MaKH; }
        }

        public bool TinhTrang
        {
            get { return tinhtrang; }
            set { tinhtrang = value; }
        }

        public HoaDon_BIZ()
        {
            tongtien = 0;
        }
        
        public HoaDon_BIZ(int _id, string _mahd, string _kh, DateTime _ngay, decimal _tien, bool _tinhtrang)
        {
            id = _id;
            maHoaDon = _mahd;
            khachhang = khachhang.Get_KH_ByCode(_kh);
            ngaylap = _ngay;
            tongtien = _tien;
            tinhtrang = _tinhtrang;
        }

        public HoaDon_BIZ Get_HoaDon_ByCode(string _MaHD)
        {
            return HoaDon_DAL.Get_HoaDon_ByCode(_MaHD);
        }

        public List<HoaDon_BIZ> GetAll_HoaDon()
        {
            return HoaDon_DAL.GetAll_HD();
        }

        public void TotalPrice()
        {
            List<CTHoaDon_BIZ> list_ctnhap = new List<CTHoaDon_BIZ>();
            list_ctnhap = CTHoaDon_DAL.GetAll_CTHoaDon_ByHD(maHoaDon);
            decimal _total = 0;
            foreach (CTHoaDon_BIZ ct in list_ctnhap)
                _total += ct.Soluong * ct.Laptop.Gia;
            tongtien = _total;
        }

        public void Save()
        {
            if (id == -1) id = HoaDon_DAL.Insert(this);
            else HoaDon_DAL.Update(this);
        }

        public void Delete()
        {
            HoaDon_DAL.Delete(this);
        }

        public int Get_Lasted_ID()
        {
            return HoaDon_DAL.Get_Lasted_ID();
        }
    }
}
